﻿using KqAutoDesign.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace KqAutoDesign.Models
{
    public delegate string GetActionDel();
    /// <summary>
    /// 代表脚本中的一个参数
    /// </summary>
    public class ScriptParam: NotifyBase,IClone<ScriptParam>
    {
        public ScriptParam()
        {

        }

        private int _id;

        public int Id
        {
            get { return _id; }
            set 
            { 
                if(_id != value)
                {
                    _id = value;
                    OnPropertyChanged(nameof(Id));
                }
            }
        }    

        private string _name;
        /// <summary>
        /// 参数名
        /// </summary>
        public string Name 
        {
            get { return _name; }
            set
            {
                if(_name != value)
                {
                    _name = value;
                    OnPropertyChanged(nameof(Name));
                }
            }
        }


        private string _value;
        /// <summary>
        /// 参数值
        /// </summary>
        public string Value 
        {
            get { return _value; }
            set
            {
                if(_value != value)
                {
                    _value = value;
                    OnPropertyChanged(nameof(Value));
                }
            }
        }

        private string _des;
        /// <summary>
        /// 参数描述
        /// </summary>
        public string Description
        {
            get { return _des; }
            set
            {
                if (_des != value)
                {
                    _des = value;
                    OnPropertyChanged(nameof(Description));
                }
            }
        }

        private ParamTypeEnum _paramTp;
        //参数类别
        public ParamTypeEnum ParamType
        {
            get { return _paramTp; }
            set
            {
                if( _paramTp != value)
                {
                    _paramTp = value;
                    OnPropertyChanged(nameof(ParamType));
                }
            }
        }

        private string _unit;
        /// <summary>
        /// 参数单位
        /// </summary>
        public string Unit
        {
            get { return _unit; }
            set
            {
                if (_unit != value)
                {
                    _unit = value;
                    OnPropertyChanged(nameof(Unit));
                }
            }
        }

        private string _defaultVal;
        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue
        {
            get { return _defaultVal; }
            set
            {
                if (_defaultVal != value)
                {
                    _defaultVal = value;
                    OnPropertyChanged(nameof(DefaultValue));
                }
            }
        }

        public ScriptParam Clone()
        {
            return MemberwiseClone() as ScriptParam;
        }
    }
    /// <summary>
    /// 参数类别枚举
    /// </summary>
    public enum ParamTypeEnum
    {
        [Description("double")]
        DoubleType,
        [Description("int")]
        IntType,
        [Description("bool")]
        BoolType,
        [Description("string")]
        StringType
    }

    /// <summary>
    /// 表示脚本中的参数
    /// </summary>
    public class ParamData
    {
        public List<KeyValuePair<int,int>> StartEnd { get; set; }=new List<KeyValuePair<int,int>>();   
        /// <summary>
        /// 参数内容，一般指的Id
        /// </summary>
        public string Content;
    }

    public class GroupScriptParam:NotifyBase
    {
        public GroupScriptParam(ScriptParam sp,string name)
        {
            ScriptParam = sp;
            GroupName = name;
        }
        private string _scName;
        public string GroupName 
        {
            get { return _scName; }
            set
            {
                if(value != _scName)
                {
                    _scName = value;
                    OnPropertyChanged(nameof(GroupName));
                }
            }
        }

        private ScriptParam _sp;
        public ScriptParam ScriptParam 
        {
            get { return _sp; }
            set
            {
                if(_sp != value)
                {
                    _sp = value;
                    OnPropertyChanged(nameof(ScriptParam));
                }
            }
        }
    }
    public class GroupParams:ObservableCollection<GroupScriptParam>
    {
        public void AddRange(IList<GroupScriptParam> list)
        {
            if (list == null) return;
            foreach(GroupScriptParam p in list)
            {
                this.Add(p);
            }
        }
    }

    public enum GlobalParamType
    {
        [Description("应用程序启动路径")]
        AppStartPath,
        [Description("脚本名称")]
        ScriptFileName,
        [Description("项目关键路径")]
        ProjectPath,
    }

    /// <summary>
    /// 全局参数字典
    /// </summary>
    public class GlobalGroupParams:GroupParams
    {
        public GlobalGroupParams()
        {

        }
        public int GetNextId()
        {
            return this.Max(x => x.ScriptParam.Id) + 1;
        }
        public bool AddNew(GroupScriptParam pm)
        {
            if (pm == null)
                return false;
            if (pm.ScriptParam == null)
                return false;
            if (string.IsNullOrEmpty(pm.GroupName))
                return false;
            if (string.IsNullOrEmpty(pm.ScriptParam.Name))
                return false;
            var existOne = this.FirstOrDefault(x => x.GroupName.Equals(pm.GroupName)
            && x.ScriptParam.Id == pm.ScriptParam.Id
            && x.ScriptParam.Name.Equals(pm.ScriptParam.Name));
            if (existOne != null)
            {
                existOne.ScriptParam = pm.ScriptParam;
            }
            else
            {
                this.Add(pm);
            }
            return true;
        }
    }
}
